﻿if (F.Form) {
    $.extend(F.Form.prototype, {
        initData: {},
        //字段只读（是否只读，字段集合）
        fieldReadnly: function (b, fields) {
            var t = this;
            b = b || false;
            function f(k, rb, list) {
                if (k.items) {
                    $.each(k.items, function (i, v) {
                        if (list) {
                            if (v.setReadonly && list.indexOf(v.id) >= 0) {
                                if (rb) {
                                    $(v.el).find("input.f-field-textbox").addClass("fieldReadnly");
                                } else {
                                    $(v.el).find("input.f-field-textbox").removeClass("fieldReadnly");
                                }
                                v.setReadonly(rb);
                            }
                            f(v, rb, list);
                        } else {
                            if (v.setReadonly) {
                                if (rb) {
                                    $(v.el).find("input.f-field-textbox").addClass("fieldReadnly");
                                } else {
                                    $(v.el).find("input.f-field-textbox").removeClass("fieldReadnly");
                                }
                                v.setReadonly(rb);
                            }
                            f(v, rb);
                        }
                    })
                }
            }
            if (fields && $.isArray(fields)) {
                f(t, b, fields);
            } else {
                f(t, b);
            }
        },
        //只读
        setReadOnly: function (cols) {
            var t = this;
            var c = [];
            //得到对应data的ID
            var datael = $(t.el).find(".f-field[data]");
            $.each(datael, function (i, v) {
                if (cols.indexOf($(v).attr('data')) >= 0) {
                    c.push($(v).attr('id'));
                    F.ui[$(v).attr('id')].setEmptyText('');
                }
            });
            t.fieldReadnly(true, c);
        },
        //得到数据 （是否验证）
        getdata: function (isv) {
            var t = this;
            var data = t.initData || {};
            if (isv) {
                if (!F.validateForm(t.id, '_self', false, false)) {
                    $(".ui-state-error").parent().css('-webkit-animation-duration', "0.3s");
                    $(".ui-state-error").parent().css('animation-duration', "0.3s");
                    $(".ui-state-error").parent().FineUIanimateCss("f-fade-in", function () { });
                    return false;
                }
            }
            var datael = $(t.el).find(".f-field[data]");
            $.each(datael, function (i, v) {
                var id = $(v).attr("id");
                var d = $(v).attr("data").replace("Data_", "");
                if (d) {
                    data[d] = F.ui[id].getValue();
                    if (F.ui[id].type == 'datepicker') {
                        data[d] = F.ui[id].getText();
                    }
                }
            })
            return data;
        },
        //设置数据
        setdata: function (data) {
            var t = this;
            t.initData = data;
            var datael = $(t.el).find(".f-field[data]");
            var dataid = $(t.el).attr("DataID");
            if (dataid && dataid != '') {
                F.ui[t.id].beforDataID = F.ui[t.id].DataID;
                F.ui[t.id].DataID = data[dataid];
            };
            $.each(datael, function (i, v) {
                var id = $(v).attr("id");
                var d = $(v).attr("data").replace("Data_", "");
                if (d && (typeof data[d]) != 'undefined') {
                    F.ui[id].setValue(data[d]);
                }
            })
        },
        //保存数据
        savedata: function (backurl, ex) {
            var t = this;
            if (!F.isSTR(backurl) && !ex) {
                ex = backurl;
            }
            //得到表单数据（是否验证）
            var d = F.ui[t.id].getdata(true);
            if (!d) return;
            var dataid = $(t.el).attr("DataID");
            d[dataid] = F.ui[t.id].DataID;
            var savems = {
                ms: JSON.stringify(d)
            };
            if (ex) {
                savems = $.extend(savems, ex);
            }
            if (F.isSTR(backurl)) {
                F.doPostBack(backurl, savems);
            } else {
                try {
                    eval('var href = window.pburl;');
                    if (href) {
                        F.doPostBack(href + "/" + t.id + 'save', savems);
                    } else {
                        F.doPostBack(t.id + 'save', savems);
                    }
                }
                catch (e) { }
            }
        }
    });
}

if (F.Grid) {
    $.extend(F.Grid.prototype, {
        editWidth: 800,//修改窗口的宽
        editHeight: 650,//修改窗口的高
        editTitle: "编辑",
        //更新行
        updaterow: function (data, rowid) {
            var t = this;
            if (!$.isArray(data)) {
                data = eval('(' + data + ')');
            }
            rowid = rowid || data[t.idField];
            for (var d in data) {
                t.updateCellValue(rowid, d, data[d]);
            }
        },
        //显示编辑页面
        showEdit: function (b, data, title, w, h) {
            var t = this;
            var editurl = F.ui[t.id].editurl;
            title = title || t.editTitle;
            w = w || t.editWidth;
            h = h || t.editHeight;
            if (!editurl) { return; }
            if (editurl.indexOf('?') < 0) {
                editurl += "?_reg=" + Math.random();
            }
            if (b) {
                var selectids = F.ui[t.id].getSelectedRows();
                if (selectids.length != 1) {
                    notifyEx("请选择一行进行编辑");
                    return;
                }
                editurl += "&id=" + selectids[0];
            }
            if (data) {
                if ($.isArray(data)) {
                    for (var d in data) {
                        editurl += "&" + d;
                    }
                } else {
                    editurl += "&" + data;
                }
            }
            F.ui[t.id + 'GridEdit'].show(editurl, title, w, h);
        },
        //重置页码
        relodenub: function () {
            var t = this;
            var nub = 0;
            //重置页码
            $.each($('#' + t.id + ' .f-grid-row'), function (i) {
                if (!$(this).hasClass("f-hidden")) {
                    var b = $(this).find('.f-grid-rownumberfield .f-grid-cell-inner');
                    b.html(nub + 1);
                    nub++;
                }
            });
        }
    });
}

(function () {
    doCallback = function (fn, args) {
        return fn.apply(this, args);
    }
    // 通知框
    notifyEx = function(msg) {
        F.notify({
            message: msg,
            messageIcon: 'information',
            target: '_top',
            header: false,
            displayMilliseconds: 3 * 1000,
            positionX: 'center',
            positionY: 'top'
        });
    }

    //确认提示
    confirmEx = function (mag, okfun) {
        F.confirm({
            message: mag,
            messageIcon: 'question',
            ok: function () {
                // 触发后台事件
                doCallback(okfun,[]);
            }
        });
    }
    //新增 修改 删除 由后台自动引用，通过传入的GirdID执行前台或者后台方法
    //新增
    GridAdd_click = function (gridid) {
        eval("var fun;try{fun=" + gridid + "new_Click;}catch(ex){}");
        if (fun) {
            fun(F.ui[gridid]);
        } else {
            F.ui[gridid].showEdit();
        }
    };
    //修改
    GridEdit_click = function (gridid) {
        eval("var fun;try{fun=" + gridid + "edit_Click;}catch(ex){}");
        var selectids = F.ui[gridid].getSelectedRows();
        if (selectids.length != 1) {
            notifyEx("请选择一行进行编辑");
            return;
        }
        if (fun) {
            fun(F.ui[gridid], selectids[0]);
        } else {
            F.ui[gridid].showEdit(true);
        }
    }
    //删除
    GridDel_click = function (pburl, gridid) {
        eval("var fun;try{fun=" + gridid + "delete_Click;}catch(ex){}");
        var selectids = F.ui[gridid].getSelectedRows();
        if (selectids.length == 0) {
            notifyEx("请选择一行进行编辑");
            return;
        }
        confirmEx("是否删除选中项", function () {
            if (fun) {
                fun(F.ui[gridid], selectids);
            } else {
                //eval('var href = pburl;');
                //F.doPostBack(href + "/" + gridid + 'Del_Click', {
                //    id: selectids,
                //    fields: F.ui[gridid].fields
                //});
                F.doPostBack(pburl + "/" + gridid + 'Del_Click', {
                    id: selectids,
                    fields: F.ui[gridid].fields
                });
            }
        });
    }
})(F)